﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HRM.Helper
{
    public class WindowsPositionHelper
    {
        public static string RegPath = @"Software\HRM\WindowBounds\";

        public static void SaveSize(Window win)
        {
            // Create or retrieve a reference to a key where the settings
            // will be stored.
            RegistryKey key;
            key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name);
            key.SetValue("Bounds", win.RestoreBounds.ToString());
            key.SetValue("Bounds",
            win.RestoreBounds.ToString(CultureInfo.InvariantCulture));
        }

        public static void SetSize(Window win)
        {
            RegistryKey key;
            key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name);
            if (key != null)
            {
                Rect bounds = Rect.Parse(key.GetValue("Bounds").ToString());
                win.Top = bounds.Top;
                win.Left = bounds.Left;
                // Restore the size only for a manually sized
                // window.
                if (win.SizeToContent == SizeToContent.Manual)
                {
                    win.Width = bounds.Width;
                    win.Height = bounds.Height;
                }
            }
        }
    }
}
